//窗口移动过度效果方法
function smoothMoveTo(win, targetX, targetY, duration = 300) {
  const startX = win.x;
  const startY = win.y;
  const startTime = performance.now();

  function animate(currentTime) {
    const elapsed = currentTime - startTime;
    const progress = Math.min(elapsed / duration, 1); // 0 → 1

    // 使用缓动函数（例如 easeOutQuad）
    const easeProgress = 1 - Math.pow(1 - progress, 2);

    // 计算当前帧的位置
    const currentX = startX + (targetX - startX) * easeProgress;
    const currentY = startY + (targetY - startY) * easeProgress;

    // 移动窗口
    win.moveTo(Math.round(currentX), Math.round(currentY));
    // 继续动画直到完成
    if (progress < 1) {
      requestAnimationFrame(animate);
    }else{
    }
  }

  requestAnimationFrame(animate);
}


 function openWithVSCode(path) {
  const process = require("child_process");
  // 规范化路径（确保使用正斜杠）
  const normalizedPath = path.replace(/\\/g, "/");
  
  try {
    const child = process.spawn("code", [normalizedPath], {
      detached: true,
      stdio: "ignore",
      shell: true,
      windowsHide: true
    });
    
    child.on('error', (err) => {
      console.error('启动VSCode失败:', err);
      alert(`无法启动VSCode: ${err.message}\n请确保VSCode已安装并已将code命令添加到系统PATH中。`);
    });
    
    child.unref();
  } catch (error) {
    console.error("启动VSCode失败:", error);
    alert(`启动VSCode失败: ${error.message}\n请确保VSCode已安装并已将code命令添加到系统PATH中。`);
  }
}

// 封装打开IDEA的函数
 function openWithIDEA(path) {
  const process = require("child_process");
  // 规范化路径（确保使用正斜杠）
  const normalizedPath = path.replace(/\\/g, "/");
  
  try {
    const child = process.spawn("idea", [normalizedPath], {
      detached: false,
      stdio: "ignore",
      shell: true,
      windowsHide: true
    });
    
    child.on('error', (err) => {
      console.error('启动IDEA失败:', err);
      alert(`无法启动IDEA: ${err.message}\n请确保IDEA已安装并已将idea命令添加到系统PATH中。`);
    });
    
    child.unref();
  } catch (error) {
    console.error("启动IDEA失败:", error);
    alert(`启动IDEA失败: ${error.message}\n请确保IDEA已安装并已将idea命令添加到系统PATH中。`);
  }
}


//防抖函数
function debounce(func, wait, immediate) {
  let timeout;
  return function () {
    const context = this;
    const args = arguments;
    const later = function () {
        timeout = null;
      if (!immediate) func.apply(context, args);
    };
    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
  };
}



const smoothMoveToD= debounce(smoothMoveTo, 100);

/**
 * 平滑调整窗口尺寸
 * @param {nw.Window} win - NW.js 窗口对象
 * @param {number} targetWidth - 目标宽度
 * @param {number} targetHeight - 目标高度
 * @param {number} [duration=300] - 过渡时间(毫秒)
 */
function smoothResize(win, targetWidth, targetHeight, duration = 300) {
    const startWidth = win.width;
    const startHeight = win.height;
    const startTime = performance.now();

    function animate(currentTime) {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);
        const easeProgress = 1 - Math.pow(1 - progress, 2); // easeOutQuad

        const currentWidth = Math.round(startWidth + (targetWidth - startWidth) * easeProgress);
        const currentHeight = Math.round(startHeight + (targetHeight - startHeight) * easeProgress);
        
        win.resizeTo(currentWidth, currentHeight);
        
        if (progress < 1) {
            requestAnimationFrame(animate);
        }
    }
    
    requestAnimationFrame(animate);
}
//加了防抖的 smoothResize
const smoothResizeD= debounce(smoothResize, 100);
